var app = angular.module('clipApp', ['clip-two']);
app.run(['$rootScope', '$state', '$stateParams', '$templateCache',
    function($rootScope, $state, $stateParams, $templateCache) {

        // Attach Fastclick for eliminating the 300ms delay between a physical tap and the firing of a click event on mobile browsers
        FastClick.attach(document.body);

        // Set some reference to access them from any scope
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;

        // custom pager
        $templateCache.get('/templates/custom-pager.html');

        // GLOBAL APP SCOPE
        // set below basic information
        $rootScope.app = {
            name: '群宴信息服务平台', // name of your project
            author: '群宴信息服务平台', // author's name or company name
            description: '群宴信息服务平台', // brief description
            version: '1.0', // current version
            year: ((new Date()).getFullYear()), // automatic current year (for copyright information)
            isMobile: (function() { // true if the browser is a mobile device
                var check = false;
                if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
                    check = true;
                };
                return check;
            })(),
            layout: {
                isNavbarFixed: true, //true if you want to initialize the template with fixed header
                isSidebarFixed: true, // true if you want to initialize the template with fixed sidebar
                isSidebarClosed: false, // true if you want to initialize the template with closed sidebar
                isFooterFixed: false, // true if you want to initialize the template with fixed footer
                theme: 'theme-5', // indicate the theme chosen for your project
                logo: 'assets/images/logo.png', // relative path of the project logo
            }
        };
        $rootScope.user = {
            name: 'Peter',
            job: 'ng-Dev',
            picture: 'app/img/user/02.jpg'
        };
    }
]);
// translate config
app.config(['$translateProvider',
    function($translateProvider) {

        // prefix and suffix information  is required to specify a pattern
        // You can simply use the static-files loader with this pattern:
        $translateProvider.useStaticFilesLoader({
            prefix: 'assets/i18n/',
            suffix: '.json'
        });

        // Since you've now registered more then one translation table, angular-translate has to know which one to use.
        // This is where preferredLanguage(langKey) comes in.
        $translateProvider.preferredLanguage('en');

        // Store the language in the local storage
        $translateProvider.useLocalStorage();

    }
]);
// Angular-Loading-Bar
// configuration
app.config(['cfpLoadingBarProvider',
    function(cfpLoadingBarProvider) {
        cfpLoadingBarProvider.includeBar = true;
        cfpLoadingBarProvider.includeSpinner = false;

    }
]);
app.factory('cookieUtil',[function(){
    return {
        getCookie:function(c_name){
            if (document.cookie.length>0)
            {
                var c_start=document.cookie.indexOf(c_name + "=");
                if (c_start!=-1)
                {
                    c_start=c_start + c_name.length+1;
                    var c_end=document.cookie.indexOf(";",c_start);
                    if (c_end==-1) {
                        c_end=document.cookie.length;
                    }
                    return unescape(document.cookie.substring(c_start,c_end));
                }
            }
            return "";
        },
        setCookie: function(c_name,value,expire){
            var exdate = new Date();
            exdate.setTime(expire * 1000);
            window.document.cookie = c_name + '=' + escape(value) + ((!expire || expire === '') ? '' : ';expires=' + exdate.toGMTString());
        },
        delCookie: function(name){
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval = this.getCookie(name);
            if(cval != ''){
                document.cookie= name + "=" + cval + ";expires="+exp.toGMTString();
            }
        }
    }
}]);
// 登录拦截器
app.factory('TokenInterceptor',['$q','$window','cookieUtil',function($q, $window,cookieUtil) {
    return {
        // 可选，拦截成功的请求
        request: function(config) {

            config.headers = config.headers || {};
            if (cookieUtil.getCookie('token') == '' && $window.location.href.indexOf('/login') < 0) {
                $window.location.href = "#/login";
            }

            config.headers.token = cookieUtil.getCookie('token');

            return config || $q.when(config);
        },

        // 可选，拦截成功的响应
        response: function(response) {

            if (response.data.code === 1 && $window.location.href.indexOf('/login') < 0) {
                $window.location.href = "#/login";
            }

            return response || $q.when(reponse);
        }
    };
}]);

// 注册拦截器
app.config(['$httpProvider',function($httpProvider){
    $httpProvider.interceptors.push('TokenInterceptor');
}]);
